package top.fangw.tenant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.fangw.tenant.pojo.dto.DatabasePageQueryDto;
import top.fangw.tenant.pojo.entity.DatabaseInfo;
import top.fangw.tenant.service.IDatabaseInfoService;
import top.fangw.common.core.result.Result;

/**
 * <p>
 * 用途：保存系统中业务数据库的信息，用于创建租户数据库使用 前端控制器
 * </p>
 *
 * @author fangw
 * @since 2024-01-16
 */
@Tag(name = "数据库信息")
@RestController
@RequestMapping("/database-info")
@RequiredArgsConstructor
public class DatabaseInfoController {

    private final IDatabaseInfoService databaseInfoService;

    @Operation(summary = "添加数据库信息")
    @PostMapping("/add")
    public Result<?> add(@RequestBody DatabaseInfo databaseInfo){
        databaseInfoService.add(databaseInfo);
        return Result.success();
    }

    @Operation(summary = "修改数据库信息")
    @PutMapping("/update")
    public Result<?> update(@RequestBody DatabaseInfo databaseInfo){
        databaseInfoService.updateInfo(databaseInfo);
        return Result.success();
    }

    @Operation(summary = "删除数据库信息")
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam String id){
        databaseInfoService.delete(id);
        return Result.success();
    }

    @Operation(summary = "获取数据库信息")
    @GetMapping("/queryById")
    public Result<?> queryById(@RequestParam String id){
        DatabaseInfo databaseInfo = databaseInfoService.queryById(id);
        return Result.success(databaseInfo);
    }

    @Operation(summary = "分页查询数据库信息")
    @PostMapping("/listPage")
    public Result<?> listPage(@RequestBody DatabasePageQueryDto databasePageQueryDto){
        IPage<DatabaseInfo> res  = databaseInfoService.listPage(databasePageQueryDto);
        return Result.success(res);
    }

}
